package session

import (
	"gitee.com/zhendliu/sorm/log"
	"gitee.com/zhendliu/sorm/schema"
	"reflect"
)

// Model assigns refTable
func (s *Session) Model(value interface{}) *Session {
	// nil or different model, update refTable
	if s.refTable == nil || reflect.TypeOf(value) != reflect.TypeOf(s.refTable.Model) {
		s.refTable = schema.Parse(s.config.Schema, *s.spliter, value)
	}
	return s
}

// Table
func (s *Session) Table(table string) *Session {
	s.refTable = schema.ParseTable(table)
	return s
}

// RefTable returns a Schema instance that contains all parsed fields
func (s *Session) RefTable() *schema.Schema {
	if s.refTable == nil {
		log.Error("Model is not set")
	}
	return s.refTable
}

// DropTable drops a table with the name of model
func (s *Session) DropTable() error {
	// _, err := s.Raw(fmt.Sprintf("DROP TABLE IF EXISTS %s", s.RefTable().Name)).Exec()
	// return err
	return nil
}
